package oojo.data.impl.exec;

import javax.persistence.EntityManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import oojo.data.Query;
import oojo.data.QueryException;
import oojo.data.spec.Create;
import oojo.data.impl.BaseQuery;

public class CreateQuery<E> extends BaseQuery<E, E> {

    private static final Logger logger = LoggerFactory.getLogger(CreateQuery.class);
    
    public Class<?> getQueryClass() {
        return Create.class;
    }
    
    public E execute(Query<E, E> create) {
        logger.debug("---> execute(spec)");
        
        try {
            Create<E> spec = (Create<E>) create;
            EntityManager entityManager = getRepository().getEntityManager();
            entityManager.persist(spec.getEntity());
            return spec.getEntity();
        } catch (Exception ex) {
            throw new QueryException(ex);
        }
    }
}
